﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CPPEI.Coolzon.ServiceProxy.DesignServiceRef;
using CPPEI.Coolzon.Common;

namespace CPPEI.Coolzon.Designer.Views
{
    /// <summary>
    /// Interaction logic for FileStatusWindow.xaml
    /// </summary>
    public partial class ViewStatusWindow : Window
    {
        private SolutionTreeViewNode _selectedNode;
        private Guid _solutionID;
        public ViewStatusWindow(Guid solutionID, SolutionTreeViewNode node)
        {
            InitializeComponent();
            _solutionID = solutionID;
            _selectedNode = node;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                CPPEI.Coolzon.ServiceProxy.DesignServiceRef.DesignServiceClient dsClient =
                    CPPEI.Coolzon.ServiceProxy.DesignServiceProxyManager.Create();
                CPPEI.Coolzon.ServiceProxy.DesignServiceRef.GetLatestStatusRequest request = new ServiceProxy.DesignServiceRef.GetLatestStatusRequest();
                request.Token = UIContext.Current.Token;
                request.SolutionId = _solutionID;
                request.Id = _selectedNode.ID;
                
                //获取服务器上项目状态
                ListResultOf_ItemStatus response = dsClient.GetItemChildrenStatus(request);
                //$开头目录不显示
                if (!_selectedNode.Name.StartsWith("$"))
                {
                    ServiceProxy.DesignServiceRef.ItemStatus itemStatus = FindItemById(response.Data, _selectedNode.ID);
                    this.Dispatcher.Invoke(() =>
                    {
                        _ctlListView.Items.Add(ConvertToModel(_selectedNode, itemStatus));
                    });
                }
                if (_selectedNode.Children != null && _selectedNode.Children.Count > 0)
                {
                    //添加显示子节点
                    ConvertChildren(_selectedNode.Children, response.Data);
                }

            }
            catch (Exception ex)
            {
                Logger.ErrorToTag(Library.LogTAG, "获取项目状态失败,ex:{0}", ex);
                Coolzon.UI.CustomMessageBox.ShowError("获取项目状态失败");
            }
        }

        /// <summary>
        /// 添加显示子节点
        /// </summary>
        /// <param name="children"></param>
        /// <param name="data"></param>
        private void ConvertChildren(ObservableCollection<SolutionTreeViewNode> children, ItemStatus[] data)
        {
            foreach (var item in children)
            {
                if (!item.Name.StartsWith("$"))
                {
                    ServiceProxy.DesignServiceRef.ItemStatus itemStatus = FindItemById(data, item.ID);
                    this.Dispatcher.Invoke(() =>
                    {
                        _ctlListView.Items.Add(ConvertToModel(item, itemStatus));
                    });
                }
                if (item.Children != null && item.Children.Count > 0)
                {
                    ConvertChildren(item.Children, data);
                }
            }
        }

        /// <summary>
        /// 把SolutionTreeViewNode对象转换成ViewStatusModel，用于绑定界面
        /// </summary>
        /// <param name="node"></param>
        /// <param name="itemStatus"></param>
        /// <returns></returns>
        private ViewStatusModel ConvertToModel(SolutionTreeViewNode node, ServiceProxy.DesignServiceRef.ItemStatus itemStatus)
        {
            ViewStatusModel model = new ViewStatusModel();
            model.ItemName = node.Name;
            model.ItemCaption = node.Caption;
            TypeToString converter = new TypeToString();

            if (itemStatus != null)
            {
                model.ItemVersion = itemStatus.ItemVersion;
                model.ItemType = (string)converter.Convert(itemStatus.ItemType, typeof(string), null, null);
                model.IsEditing = itemStatus.IsEditing;
                model.Editor = itemStatus.Editor;
                model.ExistInServer = itemStatus.ExistInServer;
                model.UpdateTime = itemStatus.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss");
            }
            else
            {
                model.ItemVersion = 0;
                model.ItemType = (string)converter.Convert(node.NodeType, typeof(string), null, null);
                model.IsEditing = true;
                model.Editor = UIContext.Current.UserName;
                model.ExistInServer = false;
                model.UpdateTime = "";
            }
            return model;
        }
        
        /// <summary>
        /// 根据id查找项目状态，未找到则返回null
        /// </summary>
        /// <param name="itemStatusArray"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        private ServiceProxy.DesignServiceRef.ItemStatus FindItemById(ServiceProxy.DesignServiceRef.ItemStatus[] itemStatusArray, Guid id)
        {
            if (itemStatusArray != null && itemStatusArray.Length > 0)
            {
                foreach (var item in itemStatusArray)
                {
                    if (string.Equals(item.ItemId, id))
                    {
                        return item;
                    }
                }
            }
            return null;
        }
    }
}
